//第3题
//基于下面提供的代码，完成后续的四个练习
//app.js


const fp = require('lodash/fp')
const { MayBe, Container } = require('./support');




//练习1：使用fp.add(x,y)和fp.map(f,x)创建一个能让functor里的值增加的函数ex1

let maybe = MayBe.of([5, 6, 1])
let ex1 = (y) => {
    //你需要现实的函数
    let add = function(x) {
        return fp.add(x, y);
    }
    return maybe.map(x => fp.map(add, x))
}
console.log(ex1(1)); //MayBe { _value: [ 6, 7, 2 ] }






//练习2：实现一个函数ex2，能够使用fp.first获取列表的第一个元素

// const fp = require('lodash/fp')
// const { MayBe, Container } = require('./support')
let xs = Container.of(['do', 'ray', 'me', 'fa', 'so', 'la', 'ti', 'do'])
let ex2 = () => {
    //你需要实现的函数
    return xs.map(x => fp.first(x));
}
console.log(ex2());




//练习3：实现一个函数ex3，使用safeProp和fp.first找到user 的名字的首字母

// const fp = require('lodash/fp')
// const { MayBe, Container } = require('./support')
let safeProp = fp.curry(function(x, o) {
    return MayBe.of(o[x])
})
let user = { id: 2, name: 'Albert' }
let ex3 = (user) => {
    //你需要实现的函数
    const name = "name";
    let oname = safeProp(name, user);
    return oname.map(x => fp.first(x));
}
console.log(ex3(user))





//练习4： 使用MayBe重写ex4， 不要有if语句

// const fp = require('lodash/fp')
// const { MayBe, Container } = require('./support')
// let ex4 = function(n) {
//     if (n) {
//         return parseInt(n);
//     }
// }

let ex4 = function(n) {
    return MayBe.of(n)
        .map(x => parseInt(x));
}
console.log(ex4(3.6));